﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.OpenApi.Models;
using Volo.Abp;
using Volo.Abp.Application;
using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Serilog;
using Volo.Abp.Autofac;
using Volo.Abp.AutoMapper;
using Volo.Abp.Domain;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.Modularity;
using Volo.Abp.Swashbuckle;
using XCWechat.Contexts;

namespace XCWechat
{
    [DependsOn(typeof(AbpAspNetCoreMvcModule),
        typeof(AbpAutofacModule),
        typeof(AbpEntityFrameworkCoreModule),
        typeof(AbpEntityFrameworkCoreMySQLModule),
        typeof(AbpDddApplicationModule),
        typeof(AbpDddDomainModule),
        typeof(AbpSwashbuckleModule),
        typeof(AbpAutoMapperModule),
        typeof(AbpAspNetCoreSerilogModule)
        )]
    public class XCWechatModule : AbpModule
    {
        public override Task ConfigureServicesAsync(ServiceConfigurationContext context)
        {
            var Configuration = context.Services.GetConfiguration();
            var service = context.Services;

            service.AddControllers(options =>
            {
                options.Filters.Add(new IgnoreAntiforgeryTokenAttribute());//忽略xss攻击
            });

            //注册DbContext上下文
            service.AddAbpDbContext<XCWechatDbContext>();

            //集成MySQL
            Configure<AbpDbContextOptions>(options =>
            {
                options.UseMySQL();
            });

            //加载AutoMapper到IOC容器
            service.AddAutoMapperObjectMapper<XCWechatDbContext>();

            //配置AutoMap
            Configure<AbpAutoMapperOptions>(options =>
            {
                options.AddMaps<XCWechatModule>();
            });

            service.AddEndpointsApiExplorer();
            service.AddAbpSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo { Title = "XCWechat API", Version = "v1" });
                options.DocInclusionPredicate((docName, api) => true); //自动加载ABP自动控制器
            });

            //配置自动API
            Configure<AbpAspNetCoreMvcOptions>(options =>
            {
                options.ConventionalControllers.Create(typeof(XCWechatModule).Assembly);
            });

            //配置API控制器异常
            Configure<AbpExceptionHandlingOptions>(options =>
            {
                options.SendExceptionsDetailsToClients = true;
            });

            return Task.CompletedTask;
        }

        public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();
            // Configure the HTTP request pipeline.
            if (env.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI();
            }

            app.UseHttpsRedirection();

            //配置abp路由
            app.UseRouting();

            app.UseAuthorization();

            app.UseConfiguredEndpoints();

            return Task.CompletedTask;
        }
    }
}
